package de.schule.barnimgymnasium.javafx;

import javafx.application.Application;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.geometry.VPos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Control;
import javafx.scene.control.TextField;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.stage.Stage;

public class Calculator extends Application {

	@Override
	public void start(final Stage primaryStage) throws Exception {
		// Die anzeigbaren Elemente (Controls oder Widgets)
		final TextField anzeige = new TextField("0");
		anzeige.setAlignment(Pos.BASELINE_RIGHT);
		anzeige.setEditable(false);
		final Button nichts = new Button("0");
		final Button eins = new Button("1");
		final Button zwei = new Button("2");
		final Button drei = new Button("3");
		final Button vier = new Button("4");
		final Button fuenf = new Button("5");
		final Button sechs = new Button("6");
		final Button sieben = new Button("7");
		final Button acht = new Button("8");
		final Button neun = new Button("9");
		final Button komma = new Button(",");
		final Button plus = new Button("+");
		final Button minus = new Button("-");
		final Button mal = new Button("*");
		final Button geteilt = new Button("/");
		final Button gleich = new Button("=");
		final Button[] tasten = { sieben, acht, neun, geteilt, vier, fuenf,
				sechs, mal, eins, zwei, drei, minus, gleich, nichts, komma,
				plus };
		for (final Control c : tasten) {
			c.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
		}
		// Die Anordnung der Elemente
		// Das Gitter vorbereiten
		final GridPane grid = new GridPane();
		// grid.setGridLinesVisible(true);
		grid.setHgap(10);
		grid.setVgap(10);
		// for (int i = 0; i < 4; i++) {
		// final ColumnConstraints cc = new ColumnConstraints();
		// cc.setPercentWidth(25);
		// grid.getColumnConstraints().add(cc);
		// }
		final ColumnConstraints col1 = new ColumnConstraints();
		col1.setPercentWidth(25);
		final ColumnConstraints col2 = new ColumnConstraints();
		col2.setPercentWidth(25);
		final ColumnConstraints col3 = new ColumnConstraints();
		col3.setPercentWidth(25);
		final ColumnConstraints col4 = new ColumnConstraints();
		col4.setPercentWidth(25);
		grid.getColumnConstraints().addAll(col1, col2, col3, col4);
		grid.setPadding(new Insets(5, 10, 15, 20));
		// Den Gitterinhalt vorbereiten
		GridPane.setColumnSpan(anzeige, 4);
		for (final Node n : tasten) {
			GridPane.setHalignment(n, HPos.CENTER);
			GridPane.setValignment(n, VPos.CENTER);
			GridPane.setHgrow(n, Priority.ALWAYS);
			GridPane.setVgrow(n, Priority.ALWAYS);
		}
		// Den Gitterinhalt ins Gitter setzen
		grid.addRow(0, anzeige);
		grid.addRow(1, sieben, acht, neun, geteilt);
		grid.addRow(2, vier, fuenf, sechs, mal);
		grid.addRow(3, eins, zwei, drei, minus);
		grid.addRow(4, gleich, nichts, komma, plus);
		// Die Anordnung der Elemente "auf die Bühne bringen"
		final Scene scene = new Scene(grid);
		primaryStage.setScene(scene);
		primaryStage.show();
	}

	/**
	 * @param args
	 */
	public static void main(final String[] args) {
		Application.launch(args);
	}

}
